﻿
namespace StationDataHub.Common;

/// <summary>
/// 平台通讯报文序列号生成器（4位自增，秒级重置）
/// </summary>
public static class SequenceGenerator
{
    private static readonly Lock _lock = new();
    private static string _lastSecond = DateTime.Now.ToString("yyyyMMddHHmmss");
    private static int _counter = 0;

    /// <summary>
    /// 获取 4 位自增序列号（同一秒内递增，下一秒重置）
    /// </summary>
    public static string NextSeq()
    {
        lock (_lock)
        {
            string currentSecond = DateTime.Now.ToString("yyyyMMddHHmmss");
            if (currentSecond != _lastSecond)
            {
                _lastSecond = currentSecond;
                _counter = 1;
            }
            else
            {
                _counter++;
                if (_counter > 9999)
                    _counter = 1; // 溢出后重置
            }
            return _counter.ToString("D4"); // 左侧补零
        }
    }
}